package com.gtq.uhome.controller;

import com.gtq.uhome.pojo.Consignee;
import com.gtq.uhome.result.Result;
import com.gtq.uhome.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: Zhong Ranjie
 * @create: 2020-08-03 17:22
 **/
@RestController
@RequestMapping(value = "/address")
@Api(tags = "用户前端收货地址")
@CrossOrigin
public class AddressController {

    @Autowired
    private AddressService findAddressService;

    /**
     * 获取用户所有收货地址
     *
     * @param userId
     * @return
     */
    @PostMapping
    @ApiOperation(value = "获取用户全部收货地址")
    public Result<List<Consignee>> consigneeAddress(@RequestParam Integer userId) {
        return Result.ok(findAddressService.findAddress(userId));
    }

    /**
     * 添加用户收货地址
     *
     * @param consignee 收件人地址详情
     * @return 成功/失败
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "添加收货地址")
    public Result consigneeAddress(@RequestBody Consignee consignee) {
        return findAddressService.addConsignee(consignee) ? Result.ok("添加成功") : Result.failed("添加失败");
    }


    /**
     * 编辑/修改收货地址
     *
     * @param consignee 收件人地址详情
     * @return 成功/失败
     */
    @PostMapping(value = "/update")
    @ApiOperation(value = "编辑收货地址")
    public Result update(@RequestBody Consignee consignee) {
        consignee.setStatusCode("0");
        return findAddressService.update(consignee) ? Result.ok("修改成功") : Result.failed("修改失败");
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除收货地址")
    public Result delete(@RequestBody Consignee consignee) {
        consignee.setStatusCode("1");
        return findAddressService.update(consignee) ? Result.ok("删除") : Result.failed("删除失败");
    }
}
